package com.ghc.androidexample1;

import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.os.ParcelFileDescriptor;
import android.provider.OpenableColumns;
import android.view.Menu;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {
	private static final int FILE_PICK_CODE = 1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	public void onShareClick(View v) {
		// Call Share activity
		Intent shareIntent = new Intent(Intent.ACTION_SEND);
		shareIntent.setType("text/plain");
		shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		shareIntent.putExtra(Intent.EXTRA_TEXT, "Send by another app");
		if (shareIntent.resolveActivity(getPackageManager()) != null) {
			startActivity(Intent.createChooser(shareIntent,
					"Choose app to share"));
		}
	}

	public void onFileClick(View v) {
		// Call Pic activity
		Intent shareIntent = new Intent(Intent.ACTION_PICK);
		shareIntent.setType("image/*");
		shareIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
		if (shareIntent.resolveActivity(getPackageManager()) != null) {
			startActivityForResult(
					Intent.createChooser(shareIntent, "Choose app to pick"),
					FILE_PICK_CODE);
		}
	}

	public void onSendDataClick(View v) {
		Intent intent = new Intent("com.ghc.androidexample");
		intent.setType("text/plain");
		intent.putExtra(Intent.EXTRA_TEXT, "Test");
		if(intent.resolveActivity(getPackageManager()) != null) {
			startActivity(intent);
		}
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if (requestCode == FILE_PICK_CODE) {
			if (resultCode == RESULT_OK) {
				Uri uri = data.getData();
				try {
					// Get File info
					Cursor cursor = getContentResolver().query(uri, null, null, null, null);
					int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
					cursor.moveToFirst();
					String name = cursor.getString(nameIndex);
					cursor.close();
					
					// Get File data
					ParcelFileDescriptor mInputPFD = getContentResolver()
							.openFileDescriptor(uri, "r");
					// Get a regular file descriptor for the file
					FileDescriptor fd = mInputPFD.getFileDescriptor();
					FileInputStream inputStream = new FileInputStream(fd);
					int length = 0;
					int total = 0;
					byte[] buffer = new byte[1024];
					while ((length = inputStream.read(buffer)) != -1) {
						total += length;
					}
					inputStream.close();
					// Show info
					TextView tvFile = (TextView)findViewById(R.id.tvFileName);
					tvFile.setText(String.format("File: %s Size: %s", name, total));
				} catch (FileNotFoundException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}

			}
		}
	}
}
